package com.qf.lanzhou.blog.api.dao.Impl;

import com.qf.lanzhou.blog.api.dao.ShowUsersDao;
import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author jian
 * @Date 2020/12/17 16:03
 * @JDK 1.8
 * @Version 1.0
 */
public class ShowUsersDaoimpl implements ShowUsersDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
    @Override
    public List<User> select(Long id) {
        String sql="select id,user_name,phone,email,age,sex,create_time from t_user where id=?";
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);
        Object[] params = {id};
        try {
            return queryRunner.query(sql,new BeanListHandler<User>(User.class, processor),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(User user) {
        String sql="update t_user set user_name=?,phone=?,email=?,age=?,sex=? where id=?";
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);
        Object[] params ={user.getUserName(),user.getPhone(),user.getEmail(),user.getAge(),user.getSex(),user.getId()};
        try {
            return queryRunner.update(sql,params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
